package com.lvmama.rhino.httpCall;

import java.io.IOException;

import javax.xml.ws.http.HTTPException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;

/**
 * 
* @ClassName: HttpClientUtil 
* @Description: TODO
* @author wanghongliang
* @date 2016年8月24日 上午11:54:35
 */
public class HttpClientUtil {
	/**
	 * 
	 * @param param1
	 * @param param2
	 * @return
	 */
	public static String getHttp(String param1,String param2){
		String responseMsg="";
		// 1.构造HttpClient的实例
		HttpClient httpClient = new HttpClient();
		// 用于测试的http接口的url
       // String url="http://localhost:8080/rhino-bridge/httpServer?param1="+param1+"&param2="+param2;
        String url="http://localhost:8080/rhino/httpserver/getCode?type=zhuanti&name=macic&channel=inner";
      
        // 2.创建GetMethod的实例
        GetMethod getMethod = new GetMethod(url);
        // 使用系统系统的默认的恢复策略
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        try {
        	//3.执行getMethod,调用http接口
        	httpClient.executeMethod(getMethod);
        	//4.读取内容
        	byte[] responseBody = getMethod.getResponseBody();
        	//5.处理返回的内容
        	responseMsg = new String(responseBody);
        	
        	//JSONObject json = (JSONObject) JSON.toJSON(responseMsg);
        	JSONObject json = JSONObject.parseObject(responseMsg);
        	System.out.println(json);
        	
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//6.释放连接
			getMethod.releaseConnection();
		}
		return responseMsg;
	}
	
	public static String postHttp(String param1,String param2){
		String responseMsg="";
		//1.构造HttpClient的实例
		HttpClient httpClient=new HttpClient();
//		httpClient.getParams().setContentCharset("GBK");
		String url="http://localhost:8080/rhino-bridge/httpServer";
		//2.构造PostMethod的实例
		PostMethod postMethod=new PostMethod(url);
		//3.把参数值放入到PostMethod对象中
		//方式1：
//		NameValuePair[] data = { new NameValuePair("param1", param1),new NameValuePair("param2", param2) };
//		postMethod.setRequestBody(data);
		//方式2： 
		postMethod.addParameter("param1", param1);
		postMethod.addParameter("param2", param2);
		
		// 4.执行postMethod,调用http接口
		try {
			httpClient.executeMethod(postMethod);
			//5.读取内容
			responseMsg = postMethod.getResponseBodyAsString().trim();
			//6.处理返回的内容
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			postMethod.releaseConnection();
		}
		return responseMsg;
	}
	
	
	public static void main(String[] args) {
		String param1="111";
		String param2="222";
		
		System.out.println("get方式调用http接口\n"+getHttp(param1, param2));
		//	System.out.println("post方式调用http接口\n"+postHttp(param1,param2));
	}
}
